package myqueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yi
 * Date: 2024-07-19
 * Time: 20:01
 */
public class MyQueue {
    private int usedsize;
    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    private ListNode front;//队头
    private ListNode rear;//队尾

    public void offer(int data){
        ListNode node = new ListNode(data);
        if(front == null){
            front = rear = node;
        }else{
            node.next = front;
            front.prev = node;
            front = node;
            this.usedsize++;
        }
    }

    public int poll(){
        if(front == null){
            System.out.println("队列没有元素！");
            return -1;
        }
        if(front == rear){
            int b = this.rear.val;
            front = null;
            rear = null;
            this.usedsize--;
            return b;
        }
        int a = this.rear.val;
        this.rear.prev.next = null;
        this.usedsize--;
        return a;
    }

    public int peek(){
        if(front == null){
            System.out.println("队列没有元素！");
            return -1;
        }
        if(front == rear){
            int b = this.rear.val;
            return b;
        }
        int a = this.rear.val;
        return a;
    }

    public int size(){
        return this.usedsize;
    }

    public boolean isEmpty(){
        return this.usedsize == 0;
    }
}
